//
// Created by xqyr on 2025/4/13.
//

#include "led.h"

#include <utility>
#include "log/log.h"
Led::Led(std::function<void( bool)> fun) :
    CharDeviceAPP("/dev/ledgpio") {
    set_devChangeCallback(std::bind(&Led::devChangeCallback,this,std::placeholders::_1));
    set_ledStatusChangeCallback(std::move(fun));
    setLed(ledStatus);
}
void Led::setLed(bool flag) {
    if(!writeDev(&flag, 1)) {
        ledStatus = flag;
    }
    else {
        ledStatus = LED_OFF;
    }
    if(ledStatusChangeCallback) {
        ledStatusChangeCallback(flag);
    }
}

bool Led::getLed() {
    uint8_t temp = ledStatus;
//    auto err = readDev(&ledStatus, 1);
//    if(  err < 0){
//        LOG_WARN("readDev error = %zd",err);
//        setLed(LED_OFF);
//    }
//    if(temp != ledStatus){
//        setLed(ledStatus);
//    }
    return ledStatus;
}

void Led::devChangeCallback(const bool) {
    LOG_INFO("Led  status : %d",getdevStatus());
}

void Led::set_ledStatusChangeCallback(std::function<void(const bool)> fun) {
    ledStatusChangeCallback = std::move(fun);
    if(ledStatusChangeCallback) {
        ledStatusChangeCallback(getLed());
    }
}


